-- max_career
-- Created by cheny3 Jan/07/2016
-- 成就：将终极称号提升至最高等级

local COMMON_FEATURE_COUNT = 3; -- 终极称号普通强化项数量

-- 内部函数声明
local whenUpgradeCareer;

-- 注册升级称号的回调
EventMgr.register("game_achievement/max_career", event.UPGRADE_CAREER, function(args)
    whenUpgradeCareer(args);
end);

-- 注册升级称号的回调
whenUpgradeCareer = function(args)
    local pet      = args.pet;
    local careerId = args.careerId;

    local careerInfo = CareerM.getCareerInfo(careerId);
    if #careerInfo["base_feature"] <= 0 then
        -- 不是终极称号，无需处理
        return;
    end

    local careerRecord = ME.user.dbase:query("max_career_record", {});
    if table.indexOf(careerRecord, careerId) ~= -1 then
        -- 已经记录过了，无需处理
        return;
    end

    local dept = CareerM.getCareerInfo(careerId, "department");
    local careerData = pet:getCareerData(dept);

    for _, info in pairs(careerData) do
        local maxLevel;
        careerInfo = CareerM.getCareerInfo(info.career) or {};

        if careerId == info.career then
            -- 如果是终极称号，分别计算每个强化项的等级，且不考虑神器强化
            for featureId = 1, COMMON_FEATURE_COUNT do
                if not info["feature"][featureId] then
                    return;
                end

                local featureInfo = careerInfo["feature" .. featureId] or {};
                maxLevel = featureInfo["max_level"] or 0;
                if info["feature"][featureId] < maxLevel then
                    return;
                end
            end
            break;
        end
    end

    -- 强化到最高等级
    table.insert(careerRecord, careerId);
    ME.user.dbase:set("max_career_record", careerRecord);

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        local config = GameAchievementM.query(id);
        local ruleArg = config["rule_arg"];

        -- 统计已达成的数量
        local count = 0;
        if type(ruleArg) == 'table' and type(ruleArg["career"]) == 'table' then
            for _, perId in pairs(ruleArg["career"]) do
                if table.indexOf(careerRecord, perId) ~= -1 then
                    count = count + 1;
                end
            end
        end

        local requireNum = ruleArg["require_num"];
        if count < requireNum then
            -- 没达到数量
            return false;
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("max_career");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, 1);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);
        local desc = config["desc"];
        local ruleArg = config["rule_arg"];
        local careers = ruleArg["career"];

        local activeCount = 0;
        local totalCount = ruleArg["require_num"];
        local activeCareers = ME.user.dbase:query("max_career_record", {});
        for i, careerId in pairs(careers) do
            -- 统计已收齐的数量
            if table.indexOf(activeCareers, careerId) ~= -1 then
                activeCount = activeCount + 1;
            end
        end

        desc = string.gsub(desc, "{require_num}", totalCount);

        -- 加上进度
        desc = desc .. "\n" .. string.format(getLocStr("achievement_progress"), activeCount, totalCount);
        return desc;
    end,
};